package com.huifeng.servlet;

import com.alibaba.fastjson.JSON;
import com.huifeng.bean.Good;
import com.huifeng.bean.ResultVo;
import com.huifeng.service.GoodService;
import com.huifeng.utils.PageUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/good")
public class GoodServlet extends BeanServlet{
    GoodService ss=new GoodService();
    ResultVo vo=null;
    public void deleteGood(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid=req.getParameter("gid");
        int row = ss.delectGood(Integer.parseInt(gid));
        if(row>0){
            vo= new ResultVo(200,"删除成功",row);
        }else{
            vo= new ResultVo(500,"删除失败",row);
        }
        String s = JSON.toJSONString(vo);
        resp.getWriter().print(s);
    }
    public void updateGood(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        //首先创建一个map集合
        Map<String, String[]> map = req.getParameterMap();
        Good good=new Good();
        //然后利用方法将他们封装起来
        BeanUtils.populate(good,map);
        //我么调用一下service的方法
        int row = ss.updateGood(good);
        //然后我们进行判断
        if(row>0){
            vo=new ResultVo(200,"恭喜您!修改成功",row);
        }else{
            vo=new ResultVo(200,"很遗憾!修改失败",row);
        }
        //最后我们将该值转为json然后传递到前端的页面上
        String s = JSON.toJSONString(vo);
        resp.getWriter().print(s);
    }
    //创建修改前回显
    public void findByShow(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid=req.getParameter("gid");
        Good good = ss.findByid(Integer.parseInt(gid));
        vo=new ResultVo(200,"查询成功",good);
        //转json
        String s = JSON.toJSONString(vo);
        resp.getWriter().print(s);
    }
    //创建添加的方法
    public void insertGood(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        //首先创建一个map集合
        Map<String, String[]> map = req.getParameterMap();
        Good good=new Good();
        //然后利用方法将他们封装起来
        BeanUtils.populate(good,map);
        //我么调用一下service的方法
        int row = ss.insertBook(good);
        //然后我们进行判断
        if(row>0){
            vo=new ResultVo(200,"恭喜您!添加成功",row);
        }else{
            vo=new ResultVo(200,"很遗憾!添加失败",row);
        }
         //最后我们将该值转为json然后传递到前端的页面上
        String s = JSON.toJSONString(vo);
            resp.getWriter().print(s);
    }
    //创建分页查询的方法
    public void FindByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //首先获取总记录数'
        int count = ss.findtotalPage();
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        PageUtils pu=new PageUtils(count,Integer.parseInt(pageSize),currentPage);
        List<Good> list = ss.FindByPage(pu);
        //存域
        pu.setList(list);
        //在实际运行过程中,我们需要给他存一个集合
        //创建一个对象,响应给用户数据
        vo=new ResultVo(200,"查询成功",pu);
        //将对象转换为json 传到前端页面
        String s = JSON.toJSONString(vo);
        //响应给前端
        resp.getWriter().print(s);
    }

}
